﻿using System;
using System.Collections.Generic;
using System.Text;

namespace System.Data
{
    public class DbFactory
    {
        public static IDbSession DbHelper(string strConn = "", DatabaseType databaseType = DatabaseType.SqlServer, string environmentName = "")
        {
            IDbSession dbHandleBase = null;
            if (string.IsNullOrEmpty(strConn))
            {
                #region 读取配置文件
                var dbOptions = ConfigHelper.Configs.GetSection("Db").Get<DbOptions>();
                switch (dbOptions.DbType)
                {
                    case DatabaseType.SqlServer:
                        dbHandleBase = new SqlServerDbSession(dbOptions.ConnectionString);
                        break;
                    case DatabaseType.MySql:
                        dbHandleBase = new MySqlDbSession(dbOptions.ConnectionString);
                        break;
                    case DatabaseType.Oracle:
                        dbHandleBase = new OracleDbSession(dbOptions.ConnectionString);
                        break;
                    case DatabaseType.Sqlite:
                        dbHandleBase = new SqliteDbSession(dbOptions.ConnectionString);
                        break;
                    default:
                        break;
                }
                #endregion
            }
            else
            {
                #region 动态配置文件
                switch (databaseType)
                {
                    case DatabaseType.SqlServer:
                        dbHandleBase = new SqlServerDbSession(strConn);
                        break;
                    case DatabaseType.MySql:
                        dbHandleBase = new MySqlDbSession(strConn);
                        break;
                    case DatabaseType.Oracle:
                        dbHandleBase = new OracleDbSession(strConn);
                        break;
                    case DatabaseType.Sqlite:
                        dbHandleBase = new SqliteDbSession(strConn);
                        break;
                    default:
                        break;
                }
                #endregion
            }
            if(dbHandleBase==null) throw new Exception("数据库配置错误");
            return dbHandleBase;
        }
    }
}
